﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using System.Diagnostics;
using System.Collections;
using System.Data;
using FirstFloor.ModernUI.Windows;
using System.Threading;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ModificarCategoria.xaml
    /// </summary>
    public partial class ModificarCategoria : UserControl, IContent
    {
        private PersistenciaDB persistencia = new PersistenciaDB();

        public ModificarCategoria()
        {
            InitializeComponent();
        }

        private async void cargarCategorias()
        {
            this.pb.IsIndeterminate = true;

            this.dgModificarCategorias.Visibility = System.Windows.Visibility.Hidden;

            this.pb.Visibility = System.Windows.Visibility.Visible;

            var slowTask = Task<DataTable>.Factory.StartNew(() => cargarProductosTask());

            await slowTask;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            this.pb.IsIndeterminate = false;

            this.dgModificarCategorias.ItemsSource = slowTask.Result.DefaultView;
            
            this.dgModificarCategorias.Visibility = System.Windows.Visibility.Visible;

        }

        private DataTable cargarProductosTask()
        {
            return persistencia.obtenerCategorias();
        }

        private void dgModificarCategorias_Row_DoubleClick
                         (object sender, MouseButtonEventArgs e)
        {
            if (this.dgModificarCategorias.SelectedItem != null)
            {
                Categoria c;

                int id;
                string nombre_categoria;
                string descripcion_categoria;
                
                try
                {
                    id = Convert.ToInt32(((DataRowView)this.dgModificarCategorias.SelectedItem).Row["id_categoria"].ToString());
                    nombre_categoria = ((DataRowView)this.dgModificarCategorias.SelectedItem).Row["nombre_categoria"].ToString();
                    descripcion_categoria = ((DataRowView)this.dgModificarCategorias.SelectedItem).Row["descripcion_categoria"].ToString();

                    c = new Categoria(id,nombre_categoria,descripcion_categoria);

                    ModificarCategoriaDialog mcd = new ModificarCategoriaDialog(c);

                    var wnd = new ModernWindow
                    {
                        Style = (Style)App.Current.Resources["EmptyWindow"],
                        Content = mcd,
                        Width = 560,
                        Height = 340,
                        Margin = new Thickness(42),
                        Padding = new Thickness(42),
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        ResizeMode = ResizeMode.NoResize
                    };
                    
                    wnd.ShowDialog();

                    this.cargarCategorias();

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {

        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {

        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            this.cargarCategorias();
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {

        }
    }
}
